package com.dev.mybatisplus.controller.base;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.dev.mybatisplus.util.ResponseResult;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import io.swagger.annotations.ApiOperation;

import javax.validation.Valid;
import java.util.List;

/**
 * 通用控制器
 * @author mini
 */
public class BaseController<S extends IService<E>, E> {

    protected S service;

    public void setService(S s) {
        this.service = s;
    }

    @ApiOperation(value = "根据主键查询实体", response = ResponseResult.class)
    @GetMapping("/getObj")
    public ResponseResult getObj(Integer id) {
        return ResponseResult.success(service.getById(id));
    }

    @ApiOperation(value = "根据条件查询列表", response = ResponseResult.class)
    @GetMapping("/list")
    public ResponseResult list(E e) {
        return ResponseResult.success(service.list(new QueryWrapper<E>(e)));
    }

    @ApiOperation(value = "根据条件分页查询列表", response = ResponseResult.class)
    @GetMapping("/page")
    public ResponseResult page(Page<E> page, E e) {
        return ResponseResult.success(service.page(page, new QueryWrapper<E>(e)));
    }

    @ApiOperation(value = "单条插入", response = ResponseResult.class)
    @PostMapping("/save")
    public ResponseResult save(@RequestBody @Valid E e) {
        return ResponseResult.toAjax(service.save(e));
    }

    @ApiOperation(value = "批量插入", response = ResponseResult.class)
    @PostMapping("/saveBatch")
    public ResponseResult saveBatch(@RequestBody List<E> e) {
        if (CollectionUtils.isEmpty(e)) {
            return ResponseResult.error();
        }
        return ResponseResult.toAjax(e.size() > 1 ? service.saveBatch(e) : service.save(e.get(0)));
    }

    @ApiOperation(value = "单条更新", response = ResponseResult.class)
    @PutMapping("/update")
    public ResponseResult update(@RequestBody E e) {
        return ResponseResult.toAjax(service.updateById(e));
    }

    @ApiOperation(value = "批量更新", response = ResponseResult.class)
    @PutMapping("/updateBatch")
    public ResponseResult updateBatch(@RequestBody List<E> e) {
        if (CollectionUtils.isEmpty(e)) {
            return ResponseResult.error();
        }
        return ResponseResult.toAjax(service.updateBatchById(e));
    }

    @ApiOperation(value = "单条删除", response = ResponseResult.class)
    @DeleteMapping("/remove")
    public ResponseResult remove(@RequestParam("id") Integer id) {
        return ResponseResult.toAjax(service.removeById(id));
    }

    @ApiOperation(value = "批量删除", response = ResponseResult.class)
    @DeleteMapping("/removeBatch")
    public ResponseResult removeBatch(@RequestParam("ids") List<Integer> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return ResponseResult.error();
        }
        return ResponseResult.toAjax(service.removeByIds(ids));
    }
}
